/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonClasses;

import commonClasses.Authentication.AeSimpleMD5;
import commonClasses.commonInterfaces.ItemIntf;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Pozytywny!
 */
public class Item implements ItemIntf {

    private String _description;
    private long _id;
    private Price _price;
    private ArrayList<String> filesPaths = new ArrayList<String>();
    private ArrayList<String> _userIdentifiers = new ArrayList<String>();
    private Boolean auctionOn = Boolean.TRUE; //dla hosta
    private String winnersHash = null;
    private Boolean auctionEntered = Boolean.FALSE; //dla klienta


    public String getDescription() {
        return this._description;
    }

    public long getId() {
        return _id;
    }

    public Price getPrize() {
        return _price;
    }

    public Item setBasic(long id, Price price, String desc) {
        this._description = desc;
        this._id = id;
        this._price = price;
        return this;
    }

    public void lookInside() {
        System.out.println("_id:" + _id + ";");
        System.out.println(
                "_price:" + _price.getValue() + ":" + _price.getCurrent() + ";");
        System.out.println("_description:" + _description + ";");
    }

    public ItemIntf addFilePath(String path) {
        this.filesPaths.add(path);
        return this;
    }

    public ArrayList<String> getFilesPaths() {
        return this.filesPaths;
    }

    public ItemIntf addUserIdentifier(String ident) {
        _userIdentifiers.add(ident);
        return this;
    }

    public ArrayList<String> getUsersIdentifiers() {
        return _userIdentifiers;
    }

    public ItemIntf removeUserIdentifier(String ident) {
        try {
            for (int i = 0; i < _userIdentifiers.size(); i++) {
                if (AeSimpleMD5.MD5(_userIdentifiers.get(i)).equals(ident)) {
                    _userIdentifiers.remove(i);
                    break;
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return this;
    }

    public ItemIntf finishAuction() {
        auctionOn = Boolean.FALSE;
        return this;
    }

    public Boolean isAuctionOnline() {
        return auctionOn;
    }

    public Boolean isAmongAuctionUsers(String hash){
        Iterator<String> it = _userIdentifiers.iterator();
        
        while(it.hasNext()) {
            String s = it.next();
            if(s.equals(hash)) return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public String getWinnersHash() {
        return winnersHash;
    }

    public ItemIntf addFilePaths(ArrayList<String> paths) {
        filesPaths.addAll(paths);
        return this;
    }

    public Price getPrice() {
        return _price;
    }

    public ItemIntf setAuctionEntered() {
        auctionEntered = true;
        return this;
    }

    public ItemIntf setAuctionOffline() {
        auctionOn = Boolean.FALSE;
        return this;
    }

    public ItemIntf setItemPrice(Price price) {
        _price = price;
        return this;
    }

    public ItemIntf setWinnersHash(String hash) {
        winnersHash = hash;
        return this;
    }

    public Boolean hasEnteredAuction() {
        return auctionEntered;
    }

}
